package com.smartwasser.yunhtgl.apphtgl.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.smartwasser.yunhtgl.apphtgl.R;
import com.smartwasser.yunhtgl.apphtgl.base.BaseActivity;
import com.smartwasser.yunhtgl.apphtgl.utils.AppConfigSP;
import com.smartwasser.yunhtgl.apphtgl.widget.GestureLockViewGroup;
import com.smartwasser.yunhtgl.apphtgl.widget.GestureLockViewGroup.OnGestureLockViewListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 设置手势
 */
public class GestureSetActivity extends BaseActivity {
    private static final int STATE_SET_GESTURE = 0;
    private static final int STATE_CONFIRM_GESTURE = 1;
    private int mState = STATE_SET_GESTURE;
    private int[] mGesturePassword;
    private int mType = 0;
    @BindView(R.id.asg_gesture_lock)
    GestureLockViewGroup mGestureLockViewGroup;
    @BindView(R.id.asg_tips)
    TextView mTipsTv;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_gesture_set);
        Intent intent = getIntent();
        if (intent != null) {
            mType = intent.getIntExtra("type", 0);
        }
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        if (mType == 1) {
            mTipsTv.setText("请输入新手势密码");
        }
        mGestureLockViewGroup.setOnGestureLockViewListener(mOnGestureLockViewListener);
    }

    @Override
    public void initData() {

    }

    private void startHomeActivity() {
        startActivity(SysActivity.class);
        this.finish();
    }

    @Override
    public void onClick(View view) {

    }

    private OnGestureLockViewListener mOnGestureLockViewListener = new OnGestureLockViewListener() {
        @Override
        public void onUnmatchedExceedBoundary() {

        }

        @Override
        public void onGestureEvent(boolean matched) {
            if (mState == STATE_CONFIRM_GESTURE) {
                mGestureLockViewGroup.validationResults(matched);
                if (matched) {
                    mTipsTv.setText("设置手势密码成功！");
                    AppConfigSP sp = new AppConfigSP(GestureSetActivity.this);
                    StringBuilder password = new StringBuilder();
                    for (int i = 0; i < mGesturePassword.length; i++) {
                        password.append(mGesturePassword[i]);
                    }
                    sp.setGesturePassword(password.toString());
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (mType == 0) {
                                startHomeActivity();
                            } else {
                                finish();
                            }
                        }
                    }, 1000);
                } else {
                    mState = STATE_SET_GESTURE;
                    mTipsTv.setText("两次绘制手势不一致，请重新绘制");
                    mState = STATE_SET_GESTURE;
                    //mGestureDisplayView.setError(true);
                    mHandler.sendEmptyMessageDelayed(-1, 1000);
                }
            }
        }

        @Override
        public void onBlockSelected(int cId) {
            //mGestureDisplayView.addChoose(cId);
        }

        @Override
        public void onDrawFinish(List<Integer> choose) {
            if (mState == STATE_SET_GESTURE) {
                int size = choose.size();
                if (size < 4) {
                    mTipsTv.setText("密码至少4位");
                    mHandler.sendEmptyMessageDelayed(-1, 1000);
                } else {
                    mGesturePassword = new int[size];
                    for (int i = 0; i < size; i++) {
                        mGesturePassword[i] = choose.get(i);
                    }
                    mHandler.sendEmptyMessageDelayed(0, 1000);
                }
                mGestureLockViewGroup.setEnabled(false);
            } else if (mState == STATE_CONFIRM_GESTURE) {
                //mGestureDisplayView.setInputOver(true);
            }
        }
    };

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 0) {
                mGestureLockViewGroup.setAnswer(mGesturePassword);
                mState = STATE_CONFIRM_GESTURE;
                mTipsTv.setText("请再次输入手势密码");
            }
            mGestureLockViewGroup.resetAndRefresh();
            //mGestureDisplayView.reset();
            mGestureLockViewGroup.setEnabled(true);
            return false;
        }
    });
}
